package com.ebt.m.utils.android;

import com.ebt.m.utils.fileutils.FileUtils;

import org.apache.http.HttpException;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
/**
 * 拼接soapheader发送请求WS信息。
 * @author ZhanWu.Li
 *
 */
public class SoapObject {

	private String wsURL = "";
	private String soapAction = "";

	private String xmlns = "";

	private String Method = "";

	private Map<String, String> params;

	private StringBuilder soapHeader;

	private String account;

	private String publicKey;
	
	private  DefaultHttpClient httpClient ;

	public SoapObject(String xmlns, String account, String key) {
		this.xmlns = xmlns;
	
		this.account = account;
		this.publicKey = key;
		soapHeader = new StringBuilder();

	}
	

	/**
	 * 拼写soap协议
	 */
	public void getSoap() {

		soapHeader = new StringBuilder();

		soapHeader.append("<soap12:Envelope ");
		soapHeader
				.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
		soapHeader.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
		soapHeader
				.append(" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\" >\n");

		soapHeader.append(getSoapHeader());
		soapHeader.append(getSoapBody());
		soapHeader.append("</soap12:Envelope>\n");

		/*if (ConfigData.ISDEBUG) {
			Log.d("SoapObject", "soapBody :" + soapHeader.toString());
		}*/
	}

	/**
	 * 拼写soapheader
	 * 
	 * @return
	 */
	public String getSoapHeader() {

		StringBuilder soapHeaderStr = new StringBuilder();

		soapHeaderStr.append("  <soap12:Header>\n");

		soapHeaderStr.append("     <EBTSoapHeader  xmlns=\"").append(xmlns)
				.append("\">\n");
		soapHeaderStr.append("    <User>").append(account)
				.append("</User>\n");
		soapHeaderStr.append("    <Sign>").append(publicKey)
				.append("</Sign>\n");
		soapHeaderStr.append("    </EBTSoapHeader >\n");
		soapHeaderStr.append("  </soap12:Header>\n");

		return soapHeaderStr.toString();

	}

	/**
	 * 拼写soapbody
	 * 
	 * @return
	 */
	public String getSoapBody() {
		StringBuilder soapBodyStr = new StringBuilder();
		soapBodyStr.append("  <soap12:Body>\n");

		soapBodyStr.append("     <").append(Method).append(" xmlns=\"")
				.append(xmlns).append("\">\n");

		Iterator iter = params.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();
			soapBodyStr.append("     <").append(key).append(">").append(val)
					.append("</").append(key).append(">\n");
		}

		soapBodyStr.append("  </").append(Method).append(">\n");
		soapBodyStr.append("  </soap12:Body>\n");

		return soapBodyStr.toString();

	}

	public String sendRequest() throws IOException, HttpException {
		getSoap();
		String responseString = "";
		URL url = new URL(this.wsURL);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.setConnectTimeout(5000);
		connection.setReadTimeout(5000);
		connection.setRequestProperty("soapaction", this.soapAction);
		connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		OutputStream outputStream = connection.getOutputStream();
		outputStream.write(soapHeader.toString().getBytes());
		outputStream.close();
		switch (connection.getResponseCode()) {
			case HttpURLConnection.HTTP_OK:
				responseString = FileUtils.getStringFromInputStream(connection.getInputStream());
				break;
			case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
				throw new ConnectTimeoutException();
			case HttpURLConnection.HTTP_UNAVAILABLE:
				throw new HttpException();
			default:
				throw new ConnectException();
		}
		return responseString;
	}

	public void setWsURL(String wsURL) {
		this.wsURL = wsURL;

	}

	public void setSoapAction(String soapAction) {
		this.soapAction = soapAction;
	}

	public String getXmlns() {
		return xmlns;
	}

	public void setXmlns(String xmlns) {
		this.xmlns = xmlns;
	}

	public String getMethod() {
		return Method;
	}

	public void setMethod(String method) {
		Method = method;
	}

	public Map<String, String> getParams() {
		return params;
	}

	public void setParams(Map<String, String> params) {
		this.params = params;
	}

	public String getWsURL() {
		return wsURL;
	}

	public String getSoapAction() {
		return soapAction;
	}

}
